<?php


class FivechessUser extends Zend_Db_Table 
{
	protected $_name = 'fivechess_user';
	protected $_primary = 'id';
	
	const WIN = 'win';
	const FAIL = 'fail';
	const DRAW = 'draw';
	
	function init()
	{
		//$this->config = new Zend_Config_Ini('./application/config/fivechess.ini', 'rule', true);
		$this->SCORE = Array(FivechessUser::WIN => 10,
							FivechessUser::FAIL => -5,
							FivechessUser::DRAW => 2);
		$this->EXP = Array( FivechessUser::WIN => 20,
							FivechessUser::FAIL =>2,
							FivechessUser::DRAW => 10);
		
	}
	
	function GetUserRow($username)
	{
		$where = $this->getAdapter()->quoteInto('username=?', $username);
		$userRow = $this->fetchRow($where);
		return $userRow;
	}
	
	function AddUserInfo($id, $username)
	{
		$userRow = $this->GetUserRow($username);
		if (!$userRow)
		{
			$nowDate = new Zend_Date();
			$value = Array('id' => $id,
						   'username' => $username,
						   'create_time' => $nowDate->get(Zend_Date::ISO_8601),
						   'update_time' => $nowDate->get(Zend_Date::ISO_8601));
			$retId = $this->insert($value);
			var_dump($retId);
		}
		return false;
	}
	
	// 游戏一次，分赢，输，和三种情况（type），增加各种战绩1次，增加或减少一定的积分，增加一定经验值。
	function AddChess($username, $type)
	{
		if (!isset($this->SCORE[$type]))	//先检查输入的类型是否在规定的范围中。
		{
			return false;
		}
		$userRow = $this->GetUserRow($username);
		if ($userRow)
		{
			$nowDate = new Zend_Date();
			echo $userRow[$type . '_count'], '<br/>';
			$value = Array($type . '_count' => $userRow[$type . '_count'] + 1,
						   'exp' => $userRow->exp + $this->EXP[$type],
						   'score' => $userRow->score + $this->SCORE[$type],
						   'update_time' => $nowDate->get(Zend_Date::ISO_8601));
			var_dump($value);
			echo '<br/>';
			$where = $this->getAdapter()->quoteInto('username=?', $username);
			echo $where, '<br/>';
			$retLine = $this->update($value, $where);
			var_dump($retLine);
			echo '<br/>';
			if ($retLine)
			{
				return true;
			}
		}
		return false;
	}
	
	// 胜利一次，增加胜利次数1，增加积分10，
	function AddWin($username)
	{
		return $this->AddChess($username, FivechessUser::WIN);
	}
	
	function AddFail($username)
	{
		return $this->AddChess($username, FivechessUser::FAIL);
	}
	
	function AddDraw($username)
	{
		return $this->AddChess($username, FivechessUser::DRAW);
	}
	
	// 用户进入房间，如果已经在该房间，那么不需要做任何变化，如果该用户已经在其他房间，那么需要退出其他房间。
	// return true:  成功进入房间
	// return false: 房间已满，不能进入。
	function IntoRoom($username, $room_id)
	{
		$userRow = $this->GetUserRow($username);
		$old_room_id = $userRow->room_id;
		if ($old_room_id == $room_id)	// 已经在该房间中。
		{
			return true;
		}
		$fcRoom = new FivechessRoom();
		$roomRow = $fcRoom->find($room_id);
		if ($roomRow->user_max > $roomRow->user_now) // 还有空闲位置
		{
			$roomRow->user_now ++;
			$userRow->room_id = $room_id;
			$roomRow->save();
			$userRow->save();
			if ($old_room_id)	// 以前是在某个房间里的，要退出房间才是。
			{
				$oldRoomRow = $fcRoom->find($old_room_id);
				$oldRoomRow->user_now --;
				$oldRoomRow->save();
			}
			return true;
		}
		return false;
	}
	
	// 退出已登陆的房间。
	function OutRoom($username)
	{
		$userRow = $this->GetUserRow($username);
		$old_room_id = $userRow->room_id;
		if ($old_room_id)	// 以前是在某个房间里的，要退出房间才是。
		{
			$oldRoomRow = $fcRoom->find($old_room_id);
			$oldRoomRow->user_now --;
			$oldRoomRow->save();
			$userRow->room_id = null;
			$userRow->save();
		}
		return true;
	}
}